#include "Density.h"

#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <vector>

using namespace std;

char* fn="../script/den0000.df3";
char filename[22];

void WriteDensity( float** density, int n )
{

	static int frame=0;

	if (frame==0)
	{
		for (int i=0;i<21;i++)
		{
			filename[i]=fn[i];
		}
		filename[21]=0;
	}

	int a=frame/1000;
	int b=(frame-a*1000)/100;
	int c=(frame%100)/10;
	int d=frame%10;

	filename[13]=a+48;
	filename[14]=b+48;
	filename[15]=c+48;
	filename[16]=d+48;

	size_t xSize=n, ySize=n, zSize=n;
	//is >> xSize >> ySize >> zSize;

	vector<char> data(xSize*ySize*zSize + 6, 0);

	data[0] = xSize/256; data[1] = xSize%256;
	data[2] = ySize/256; data[3] = ySize%256;
	data[4] = zSize/256; data[5] = zSize%256;

	for (int i=0;i<xSize;i++)
	{
		for (int j=0;j<ySize;j++)
		{
			if (density[i][j]>0)
			{
				printf("%f\n",density[i][j]);
			}
			for (int k=0;k<zSize;k++)
			{

				data.at(i + j*xSize + k*xSize*ySize + 6)=density[i][j]*100;
				
			}
		}
	}

	ofstream os(filename, ios::binary);
	os.write(&data[0], data.size());

	frame++;
}
